<template>
  <div>
    <div class="main_content">
      <el-form :inline="true" ref="s_form" :model="s_form">
        <el-form-item label="单元名称：" prop="firstName">
          <el-input
            style="width:200px;" size='medium'
            clearable
            v-model="s_form.name"
            placeholder="请输入查询单元名称"
            @keydown.enter.native="doSearch()"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="added" @click="doSearch()">
            <i class="el-icon-search"></i>
            搜索
          </el-button>
          <el-button v-if="showBack" @click="resetForm()">
            <i class="el-icon-back"></i>
            返回
          </el-button>
        </el-form-item>
        <el-button type="primary" @click="toAdd()">
          <i class="el-icon-circle-plus-outline"></i>
          新增
        </el-button>
      </el-form>
      <myTable
        auto='getAllList'
        style='margin-top:15px'
        :height="Height-155"
        min-width='130'
        :myColumn=tableProp
        ahide='index,checkbox,mySplitPage,tooltip,background'
        hide="checkbox"
        slots='操作,250,right'
        select='console'
        hook:created='myTable.tableData = tableData'>
        <template slot-scope='button'>
          <el-button type="primary" size="mini" @click.stop="handleEdit(button.scope.$index, button.scope.row)">
            <i class="el-icon-edit"></i>
            编 辑
          </el-button>
          <el-button type="primary" size="mini" @click="handleDelete(button.scope.$index, button.scope.row)">
            <i class="el-icon-edit"></i>
            删 除
          </el-button>
        </template>
      </myTable>
    </div>
    <!--删除-->
    <el-dialog v-dialogDrag ref="dialog_isDelete" title="提示" :visible.sync="isDelete" width="32%">
      <span>是否删除！</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doDelete()">确 定</el-button>
      </span>
    </el-dialog>
    <!--新增-->
    <el-dialog v-dialogDrag :title="addOrEdit" :visible.sync="isAdd" width="60%" :close-on-click-modal=false
               :before-close='closeDialog'>
      <el-form id="new_project" :model="form" :inline="true" :rules="rules" ref="ruleForm" label-width="200px">
        <el-form-item label="单元名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入单元名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否有效" prop="state">
          <el-select v-model="form.state" clearable>
            <el-option
              v-for="item in allsign"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归属项目" prop="projectId">
          <el-select v-model="form.projectId" clearable>
            <el-option
              :label="no"
              :value="0">
            </el-option>
            <el-option
              v-for="item in projectList"
              :key="item.projectId"
              :label="item.projectName"
              :value=item.projectId>
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="专业1" prop="major1Id">-->
<!--          <el-select v-model="form.major1Id" clearable @change="getMajor2List(form.major1Id)">-->
<!--            <el-option-->
<!--              :label="no"-->
<!--              :value="0">-->
<!--            </el-option>-->
<!--            <el-option-->
<!--              v-for="item in major1List"-->
<!--              :key="item.majorId"-->
<!--              :label="item.majorName"-->
<!--              :value=item.majorId>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="专业2" prop="major2Id">-->
<!--          <el-select v-model="form.major2Id" clearable  @change="getMajor3List(form.major2Id)">-->
<!--            <el-option-->
<!--              :label="no"-->
<!--              :value="0">-->
<!--            </el-option>-->
<!--            <el-option-->
<!--              v-for="item in major2List"-->
<!--              :key="item.majorId"-->
<!--              :label="item.majorName"-->
<!--              :value=item.majorId>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="专业3" prop="major3Id">-->
<!--          <el-select v-model="form.major3Id" clearable>-->
<!--            <el-option-->
<!--              :label="no"-->
<!--              :value="0">-->
<!--            </el-option>-->
<!--            <el-option-->
<!--              v-for="item in major3List"-->
<!--              :key="item.majorId"-->
<!--              :label="item.majorName"-->
<!--              :value=item.majorId>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doAdd('ruleForm')">确 定</el-button>
        <el-button @click="addFalse()">取 消</el-button>
      </span>
    </el-dialog>
    <!--编辑-->
    <el-dialog v-dialogDrag :title="addOrEdit" :visible.sync="isEdit" width="45%" :close-on-click-modal=false>
      <el-form id="new_project" :model="theEdit" :inline="true" label-width="200px">
        <el-form-item label="单元名称" prop="name">
          <el-input v-model="theEdit.name" placeholder="请输入单元名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否有效" prop="state">
          <el-select v-model="theEdit.state" clearable>
            <el-option
              v-for="item in allsign"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归属项目" prop="projectId">
          <el-select v-model="theEdit.projectId" clearable>
            <el-option
              :label="no"
              :value="0">
            </el-option>
            <el-option
              v-for="item in projectList"
              :key="item.projectId"
              :label="item.projectName"
              :value=item.projectId>
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="专业1" prop="major1Id">-->
<!--          <el-select v-model="theEdit.major1Id" clearable @change="getMajor2List(form.major1Id)">-->
<!--            <el-option-->
<!--              :label="no"-->
<!--              :value="0">-->
<!--            </el-option>-->
<!--            <el-option-->
<!--              v-for="item in major1List"-->
<!--              :key="item.majorId"-->
<!--              :label="item.majorName"-->
<!--              :value=item.majorId>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="专业2" prop="major2Id">-->
<!--          <el-select v-model="theEdit.major2Id" clearable  @change="getMajor3List(form.major2Id)">-->
<!--            <el-option-->
<!--              :label="no"-->
<!--              :value="0">-->
<!--            </el-option>-->
<!--            <el-option-->
<!--              v-for="item in major2List"-->
<!--              :key="item.majorId"-->
<!--              :label="item.majorName"-->
<!--              :value=item.majorId>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="专业3" prop="major3Id">-->
<!--          <el-select v-model="theEdit.major3Id" clearable>-->
<!--            <el-option-->
<!--              :label="no"-->
<!--              :value="0">-->
<!--            </el-option>-->
<!--            <el-option-->
<!--              v-for="item in major3List"-->
<!--              :key="item.majorId"-->
<!--              :label="item.majorName"-->
<!--              :value=item.majorId>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editFunction()">确 定</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import myhttp, {
  isEmpty,
  msgError,
  msgSuccess,
  msgWarning
} from "../../utils/myhttp";
import qs from "qs";
import api from "../../utils/api";

export default {
  data () {
    return {
      loading: "",
      tableHeaderStyle: {
        //表头样式
        "background-color": "rgb(242,242,242)"
      },
      showBack: false,
      isDelete: false, //是否删除
      isEdit: false,
      sType: [],//二级分类下拉框
      no: "无",
      vno: "0",
      items: [],//存放所有分类数据
      theRow: {}, //当前行数据
      theEdit: {}, //当前行数据
      theIndex: 0, //当前下标
      searchText: "", //搜索字段
      myTotal: 1, //分页总条数，默认1
      currentPage: 1, //初始化当前页
      nowPage: 1, //当前页
      thePageSize: 15, //每页条数
      tableData: [],
      projectList: [],//项目集合
      addOrEdit: '新增分类',
      isAdd: false, //是否新增
      s_form: {
        name: ""
      },
      myPic: [],
      form: {
        name: "",
        state: "",
        parentId: "",
        major1Id: null,
        major2Id: null,
        major3Id: null
      },
      tableProp: [
        {label: "名称", prop: "name"},
        {label: "项目名称", prop: "projectName"},
        // {label: "专业名称", prop: "majorName"},
        {
          label: "创建时间", prop: "crtTime", html (prop) {
            return new Date(prop).toLocaleString().replace(/:\d{1,2}$/, ' ')
          }
        },
        {
          label: "是否有效", prop: "state", html (prop) {
            return prop === 'EFFECTIVE' ? '是' : '否';
          }
        },
      ],
      allsign: [
        {value: 'EFFECTIVE', label: "是"},
        {value: 'INVALID', label: "否"}
      ],
      major1List: [],
      major2List: [],
      major3List: [],
      Classify: [],
      rules: {
        //表单验证
        name: [
          {required: true, message: "必填项", trigger: "blur"},
        ],
        state: [
          {required: true, message: "必填项", trigger: "blur"}
        ],
        projectId: [
          {required: true, message: "必填项", trigger: "blur"}
        ],
        major1Id: [
          {required: true, message: "必填项", trigger: "blur"}
        ],
        major2Id: [
          {required: true, message: "必填项", trigger: "blur"}
        ],
        major3Id: [
          {required: true, message: "必填项", trigger: "blur"}
        ],
      },
    };
  },
  components: {},
  created () {
    this.getprojectList()
    this.getAllList()
  },
  methods: {
    getprojectList: async function () {
      var res = await $.post('http://yangzidaxingdiaozhuang.com/pxjy/app/proProject/getList2', {userId: localStorage.getItem("userId")});
      this.projectList = res.items;
    },
    handleDelete (index, row) {
      this.isDelete = true;
      this.theRow = row;
    },
    doDelete: async function () {
      var res = await $.post('http://yangzidaxingdiaozhuang.com/pxjy/system/pzUnits/delById', {
        id: this.theRow.id
      });
      if (res.success) return $.msgError(res.msg || 'error!');
      $.msgSuccess(res.msg || 'success!');
      this.isDelete = false;
      this.getAllList();
    },
    getAllList: async function (pageNum, pageSize) {
      var res = await $.post('http://yangzidaxingdiaozhuang.com/pxjy/system/pzUnits/getPage', {
        offset: pageNum, //当前页
        limit: pageSize,//每页显示条数
        name: this.s_form.name,//搜索一级分类
        userId: localStorage.getItem("userId")
      });
      this.items = res.items;
      $return('__', res);
    },
    //返回显示全部数据
    resetForm () {
      this.s_form.name = "";
      this.getAllList();
      this.showBack = false;
    },
    //编辑
    handleEdit: async function (index, row) {
      //调起编辑弹窗
      var res = await $.post('http://yangzidaxingdiaozhuang.com/pxjy/system/pzUnits/getInfoById', {
        id: row.id
      });
      this.Classify = res.items;
      this.theEdit = row;
      // this.getMajor2List(this.theEdit.major1Id)
      // this.getMajor3List(this.theEdit.major2Id)
      this.addOrEdit = "编辑";
      this.isEdit = true;
    },
    cancel () {
      this.isEdit = false
      this.myPic.splice(0, this.myPic.length)
    },
    doAdd (formName) {
      //删除或新增
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.addOrEdit == "新增") {
            this.addFunction(); //新增
          }
        } else {
          msgWarning(this, "请填写必要信息！");
          return false;
        }
      });
    },
    //新增
    addFunction: async function () {
      var res = await $.post('http://yangzidaxingdiaozhuang.com/pxjy/system/pzUnits/toAdd', {
        name: this.form.name,
        state: this.form.state,
        projectId:this.form.projectId
      });
      if (res.success) return $.msgError(res.msg || 'error!');
      this.isAdd = false;
      this.getAllList();
      this.$refs['ruleForm'].resetFields();
    },
    //修改
    editFunction: async function () {
      var res = await $.post('http://yangzidaxingdiaozhuang.com/pxjy/system/pzUnits/toAdd', {
        id: this.theEdit.id,
        name: this.theEdit.name,
        state: this.theEdit.state,
        projectId:this.theEdit.projectId
      });
      if (res.success) return $.msgError(res.msg || 'error!');
      this.isEdit = false;
      this.myPic.splice(0, this.myPic.length)
      this.getAllList();
    },
    //新增取消
    addFalse () {
      this.isAdd = false;
      this.$refs['ruleForm'].resetFields();
    },
    closeDialog () {
      this.$nextTick(() => {
        this.isAdd = false;
        this.$refs["ruleForm"].resetFields();
      });
    },
    toAdd: async function () {
      this.addOrEdit = "新增";
      this.isAdd = true;
    },
    doSearch () {
      //搜索方法
      this.getAllList();
      this.showBack = true;
    },
  },

  mounted () {
  }
};
</script>
